//
//  HWRechargePackageView.m
//  嗨文
//
//  Created by 响当当 on 2019/2/14.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWRechargePackageView.h"
#import "HWRechargePackageButton.h"
#import "HWRechargeNotesViewController.h"
#import "HWStraipManager.h"

@interface HWRechargePackageView()
@property (nonatomic, strong) UIButton *weixinPayButton;//微信支付
@property (nonatomic, strong) UIButton *aPayButton;//支付宝支付
@property (nonatomic, strong) UIButton *rechargeMoneyButton;//开通

@property (nonatomic, strong)NSMutableArray *moneyBtnArr;//
@property (nonatomic, strong)HWRechargeVIPModel *vipModel;//
@property (nonatomic, assign)NSInteger vipNum;//
@property (nonatomic, strong)NSString *rechargeId;//

@property (nonatomic, strong)HWStraipManager *IAPManager;

@property (nonatomic, strong)NSString *hide;//

@end

@implementation HWRechargePackageView

- (NSMutableArray *)moneyBtnArr
{
    if (!_moneyBtnArr)
    {
        _moneyBtnArr = [[NSMutableArray alloc]init];
    }
    return _moneyBtnArr;
}

- (UIButton *)weixinPayButton
{
    if (!_weixinPayButton)
    {
        _weixinPayButton = [HWUtilties creatButtonWithFrame:CGRectMake(CGRectGetMaxX(self.aPayButton.frame)+15*kUIScaleBasePlus,245*kUIScaleBasePlus,90*kUIScaleBasePlus,58*kUIScaleBasePlus)
                                               textAligment:NSTextAlignmentCenter
                                                       text:@"微信"
                                                   textClor:LCHexColor(0x47C961)
                                                  buttonBGC:[UIColor clearColor]
                                                   textFont:10*kUIScaleBasePlus
                                                     target:self
                                                     method:@selector(weixinPay:)];
        [_weixinPayButton setImage:[UIImage imageNamed:@"微信"] forState:UIControlStateNormal];
        [_weixinPayButton setUpImageAndDownLableWithSpace:1];
        _weixinPayButton.layer.masksToBounds = YES;
        _weixinPayButton.layer.cornerRadius = 4;
        _weixinPayButton.layer.borderWidth = 1;
        _weixinPayButton.layer.borderColor = LCHexColor(0x89898C).CGColor;
        _weixinPayButton.hidden = YES;

    }
    return _weixinPayButton;
}

- (UIButton *)aPayButton
{
    if (!_aPayButton)
    {
        _aPayButton = [HWUtilties creatButtonWithFrame:CGRectMake(15*kUIScaleBasePlus,245*kUIScaleBasePlus,90*kUIScaleBasePlus,58*kUIScaleBasePlus)
                                          textAligment:NSTextAlignmentCenter
                                                  text:@"支付宝"
                                              textClor:LCHexColor(0x07A0F8)
                                             buttonBGC:[UIColor clearColor]
                                              textFont:12*kUIScaleBasePlus
                                                target:self
                                                method:@selector(aPay:)];
        [_aPayButton setImage:[UIImage imageNamed:@"支付宝"] forState:UIControlStateNormal];
        [_aPayButton setUpImageAndDownLableWithSpace:1];
        _aPayButton.layer.masksToBounds = YES;
        _aPayButton.layer.cornerRadius = 4;
        _aPayButton.layer.borderWidth = 1;
        _aPayButton.layer.borderColor = LCHexColor(0x07A0F8).CGColor;
        _aPayButton.hidden = YES;
    }
    return _aPayButton;
}

- (UIButton *)rechargeMoneyButton
{
    if (!_rechargeMoneyButton)
    {
        _rechargeMoneyButton = [HWUtilties creatButtonWithFrame:CGRectMake(267*kUIScaleBasePlus,253*kUIScaleBasePlus,86*kUIScaleBasePlus,42*kUIScaleBasePlus)
                                                   textAligment:NSTextAlignmentCenter
                                                           text:@"立即开通"
                                                       textClor:LCHexColor(0xFFFFFF)
                                                      buttonBGC:LCHexColor(0xF54337)
                                                       textFont:16*kUIScaleBasePlus
                                                         target:self
                                                         method:@selector(openVip)];
        _rechargeMoneyButton.layer.masksToBounds = YES;
        _rechargeMoneyButton.layer.cornerRadius = 4;
    }
    return _rechargeMoneyButton;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.vipNum = 121;
        self.rechargeId = @"11";
        [self addSubview:self.aPayButton];
        [self addSubview:self.weixinPayButton];
        [self addSubview:self.rechargeMoneyButton];
        
        [self attention];
        
    }
    return self;
}

#pragma mark - 充值金额按钮创建
- (void)creatUI:(HWRechargeVIPModel *)model
{
    UILabel *titleLabel = [HWUtilties creatLabelWithFrame:CGRectMake(16*kUIScaleBasePlus,23*kUIScaleBasePlus,120*kUIScaleBasePlus,20*kUIScaleBasePlus)
                                             textAligment:NSTextAlignmentLeft
                                                 textClor:LCHexColor(0x000000)
                                                 textFont:22*kUIScaleBasePlus];
    titleLabel.text = @"会员套餐";
    [self addSubview:titleLabel];
    
    UIButton *rechargeRecordBtn = [HWUtilties creatButtonWithFrame:CGRectMake(CGRectGetMaxX(titleLabel.frame)+170*kUIScaleBasePlus,30*kUIScaleBasePlus,50*kUIScaleBasePlus,12*kUIScaleBasePlus)
                                                      textAligment:NSTextAlignmentLeft
                                                              text:@"充值记录"
                                                          textClor:LCHexColor(0x545454)
                                                         buttonBGC:[UIColor clearColor]
                                                          textFont:12*kUIScaleBasePlus
                                                            target:self
                                                            method:@selector(gotoRechargeRecord)];
    [self addSubview:rechargeRecordBtn];
    
    CGFloat width = 160.0*kUIScaleBasePlus;
    CGFloat height = 80.0*kUIScaleBasePlus;
    
    CGFloat margin = 15*kUIScaleBasePlus;
    
    for (int i=0; i<4; i++)
    {
        HWRechargePackageButton *button = [[HWRechargePackageButton alloc]initWithFrame:CGRectMake(margin+(i%2)*(margin+width),(i/2)*(margin+height)+CGRectGetMaxY(titleLabel.frame)+17*kUIScaleBasePlus,width,height)];
        [button getRechargeDate:model.goodsList[i] isChangeCoin:self.hide];
        button.tag = i;
        if (i==0)
        {
            [button changeColor:0];
        }
        [self.moneyBtnArr addObject:button];
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
}

#pragma mark - 温馨提示
- (void)attention
{
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(16*kUIScaleBasePlus,318*kUIScaleBasePlus,3*kUIScaleBasePlus,15*kUIScaleBasePlus)];
    lineView.backgroundColor = LCHexColor(0xF54337);
    [self addSubview:lineView];
    
    UILabel *titleLabel = [HWUtilties creatLabelWithFrame:CGRectMake(CGRectGetMaxX(lineView.frame)+10*kUIScaleBasePlus,318*kUIScaleBasePlus, 120*kUIScaleBasePlus,14*kUIScaleBasePlus)
                                             textAligment:NSTextAlignmentLeft
                                                 textClor:LCHexColor(0x333333)
                                                 textFont:15*kUIScaleBasePlus];
    titleLabel.text = @"充值说明";
    [titleLabel setFont:[UIFont boldSystemFontOfSize:15*kUIScaleBasePlus]];
    [self addSubview:titleLabel];
    
    UILabel *contextLabel = [HWUtilties creatLabelWithFrame:CGRectMake(16*kUIScaleBasePlus,CGRectGetMaxY(titleLabel.frame)+19*kUIScaleBasePlus,344*kUIScaleBasePlus,91*kUIScaleBasePlus)
                                               textAligment:NSTextAlignmentLeft
                                                   textClor:LCHexColor(0x595656)
                                                   textFont:13*kUIScaleBasePlus];
    contextLabel.numberOfLines = 0;
    contextLabel.text = @"1.充值遇到任何问题，均可联系客服获得帮助。请关注微信公众号【嗨星球】\n2.部分书籍因版权限制，不支持VIP免费阅读\n3.包月服务不支持退款，敬请谅解 ";
    [contextLabel sizeToFit];
    [self addSubview:contextLabel];
}

#pragma mark - 选中充值按钮状态展现
- (void)btnClick:(UIButton *)sender
{
   
    [self.moneyBtnArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        HWRechargePackageButton *button = obj;
        if (idx == sender.tag)
        {
            [button changeColor:0];
        }
        else
        {
            [button changeColor:1];
        }
    }];
    
    self.rechargeId = self.vipModel.goodsList[sender.tag][@"rechargeId"];
    
    switch (sender.tag)
    {
        case 0:
        {
            self.vipNum = 121;
        }
            break;
        case 1:
        {
            self.vipNum = 251;
        }
        break;
        case 2:
        {
            self.vipNum = 501;
        }
            break;
        case 3:
        {
            self.vipNum = 881;
        }
            break;
        default:
            break;
    }
}

#pragma mark - 微信支付
- (void)weixinPay:(UIButton *)sender
{
    
    self.aPayButton.layer.borderColor = LCHexColor(0x89898C).CGColor;
    self.weixinPayButton.layer.borderColor = LCHexColor(0x47C961).CGColor;
    
}

#pragma mark - 支付宝支付
- (void)aPay:(UIButton *)sender
{
    
    self.weixinPayButton.layer.borderColor = LCHexColor(0x89898C).CGColor;
    self.aPayButton.layer.borderColor = LCHexColor(0x07A0F8).CGColor;
    
}

#pragma mark - 充值
- (void)openVip
{
    if ([self.hide isEqualToString:@"1"])
    {
        [self purchaseAction];
    }
    else
    {
        
        NSArray *arr = [[NSArray alloc]init];
        
        arr = self.vipModel.goodsList;
        
        NSDictionary *dic = arr[self.vipNum];
        
        NSNumber *num = IFNULL(dic[@"rechargeId"],[NSNumber numberWithInt:0]);
        
        NSDictionary *prarmers = @{
                                   @"access_token" : current_User.token,
                                   @"userId" : self.vipModel.userId,
                                   @"rechargeId" : [num stringValue]
                                   };
        
        [NetWorkingManager postRequestUrl:FULL_WEB_URL(kAPI_AIPAYVIP) withPramater:prarmers sucess:^(id responseObject) {
            [HWUtilties openUrlSchedule:responseObject[@"data"]];
        } failure:^(NSError *error) {
            
        }];
    }

}


- (void)purchaseAction
{
    [SVProgressHUD showWithStatus:@"加载中"];

    if (!_IAPManager)
    {
        _IAPManager = [HWStraipManager shareSIAPManager];
    };
    
    WeakSelf(self)
    // iTunesConnect 苹果后台配置的产品ID
    [_IAPManager startPurchWithID:[NSString stringWithFormat:@"%@.%ld",@"by",self.vipNum]
                       rechargeId:self.rechargeId
                          payType:@"3"
                   completeHandle:^(SIAPPurchType type, NSData *data) {
                       [SVProgressHUD dismiss];
                       if (type == SIAPPurchVerSuccess)
                       {
                           if (weakSelf.paySuccessBlock)
                           {
                               weakSelf.paySuccessBlock();
                           }
                       }
                   }];
}

#pragma mark - 充值记录
- (void)gotoRechargeRecord
{
    HWRechargeNotesViewController *vc = [[HWRechargeNotesViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.type = @"0";
    [[HWUtilties getCurrentVC].navigationController pushViewController:vc animated:YES];
 
}

#pragma mark - 获取数据
- (void)getRechargeDate:(HWRechargeVIPModel *)model
{
    [self creatUI:model];
    self.vipModel = model;
}

- (void)hideWexiOrAliPay:(NSString *)hide
{
    self.hide = hide;
    if ([hide isEqualToString:@"1"])
    {
        self.aPayButton.hidden = YES;
        //        self.weixinPayButton.hidden = YES;
    }
    else
    {
        self.aPayButton.hidden = NO;
        //        self.weixinPayButton.hidden = NO;
    }
}
@end
